package com.xez.data
{
	import com.xez.util.FsConst;
	import com.xez.util.ViewUtil;
	
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class DirRoot extends CacheData
	{
		public function DirRoot()
		{
		}
		
		[Bindable]
		public var curImgRoot:ImgRoot;
		
		[Bindable]
		public var pageMsg:String = '0/0';
		
		public var webname:String;
		public var name:String;
		public var imgs:ArrayCollection;
		public var isvip:Boolean = false;
		
		private var loaded:Boolean = false;
		private var imgIndex:int = 0;
		
		override public function get cachename():String
		{
			return webname + '_' + name;
		}
		
		public function get label():String
		{
			return name;
		}
		
		public function get tip():String
		{
			var s:String = '';
			/*if (isvip)
				s = '<Vip>';
			else
				s = '';*/
			
			if (isnew)
				s = '<新>';	
			
			return s;
		}
		
		public function load():void
		{
			if (loaded)
				return;
			
			loaded = true;
			
			if (hasCache())
				loadImgsFromCache();
			
			if (!imgs || imgs.length == 0)
				msg = '正在加载数据';
			
			loadImgsFromUrl();
		}
		
		public function refresh():void
		{
			msg = '正在刷新数据';
			loadImgsFromUrl();
		}
		
		public function goPreImg():void
		{
			imgIndex--;
			disImg();
		}
		
		public function goNextImg():void
		{
			imgIndex++;
			disImg();
		}
		
		private function disImg():void
		{
			if (imgs.length == 0)
			{
				ViewUtil.tipShort('目录下没有图片');
				return;
			}
			else if (imgIndex < 0)
			{
				imgIndex = 0;
				ViewUtil.tipShort('已经是第一页啦');
			}
			else if (imgIndex >= imgs.length)
			{
				imgIndex = imgs.length - 1;
				ViewUtil.tipShort('已经是最后页啦');
			}
			
			pageMsg = (imgIndex + 1) + '/' + imgs.length;
			
			var imgRoot:ImgRoot = imgs[imgIndex] as ImgRoot;
			if (!curImgRoot || curImgRoot.cachename != imgRoot.cachename)
			{
				curImgRoot = imgRoot;
				curImgRoot.load();
			}
		}
		
		private function loadImgsFromUrl():void
		{
			var hs:HTTPService = new HTTPService();
			hs.resultFormat = 'text';
			hs.addEventListener(ResultEvent.RESULT, getimg_resultHandler);
			hs.addEventListener(FaultEvent.FAULT, getimg_faultHandler);
			hs.url = FsConst.GET_IMG_URL + '?dirname=' + name;
			hs.method = 'POST';
			hs.send();
		}
		
		private function loadImgsFromCache():void
		{
			trace('load from cache', name);
			imgs = new ArrayCollection();
			var imgObjs:ArrayCollection;
			var bytes:ByteArray = getCache();
			
			if (bytes)
				imgObjs = getCache().readObject() as ArrayCollection;
			
			if (imgObjs)
			{
				var img:ImgRoot;
				for each (var imgObj:Object in imgObjs)
				{
					img = new ImgRoot();
					img.name = imgObj.name;
					img.dirname = name;
					img.webname = webname;
					img.url = imgObj.url;
					imgs.addItem(img);
				}
			}
			
			disImg();
		}
		
		private function getimg_resultHandler(e:ResultEvent):void
		{
			trace('load result', name);
			msg = null;
			
			var imgxml:XML = new XML(e.result);
			trace(name, e.result);
			if (imgxml)
			{
				imgs = new ArrayCollection();
				var img:ImgRoot;
				for (var i:int = 0; i < imgxml.img.length(); i++)
				{
					// 设置图片数据
					img = new ImgRoot();
					img.dirname = name;
					img.webname = webname;
					img.name = imgxml.img[i].name;
					img.url = imgxml.img[i].url;
					imgs.addItem(img);
				}
				
				disImg();
				
				// 缓存数据
				var bytes:ByteArray = new ByteArray();
				bytes.writeObject(imgs);
				saveCache(bytes);
			}
		}
		
		private function getimg_faultHandler(e:FaultEvent):void
		{
			trace('load fault', name);
			msg = null;
		}
		
	}
}